<?php
include_once('inc/functions.inc.php');

if(!isEmpty(getParam('operation'))){
    
    switch(getParam('operation')){
        case 'contenido': {
            fntAcademicoGradosContenido($db);
            break;
        }
        case 'reporte': {
            fntAcademicoGradosReporte($db);
            break;
        }
        case 's': {
            fntAcademicoGradosGrabar($db);
            break;
        }
    }
    die();
}

function fntAcademicoGradosContenido($db){
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    $arrSecciones = array();
    //VARIBLES
    
    $strId = isset($_POST["hdnAcademicoGrados_Id"]) ? $_POST["hdnAcademicoGrados_Id"] : "";
    if( !empty($strId) ) {
        $strQuery = "SELECT * FROM cllg_academico_grados WHERE ID = '{$strId}'";
        $qTMP = $db->query($strQuery);
        while ( $rTMP = $db->fetch_array($qTMP) ) { 
            $arrInfo = $rTMP;        
        }
        $db->free_result($qTMP);
        $strQuery = "SELECT * FROM cllg_academico_grados_secciones WHERE FK_GRADO = '{$strId}'";
        $qTMP = $db->query($strQuery);
        while ( $rTMP = $db->fetch_array($qTMP) ) { 
            $arrInfoSecciones[$rTMP["FK_SECCION"]] = $rTMP;        
        }
        $db->free_result($qTMP);
    }
    
    $strCodigo = isset($arrInfo["CODIGO"]) ? $arrInfo["CODIGO"] : "";
    $strNombre = isset($arrInfo["NOMBRE"]) ? $arrInfo["NOMBRE"] : "";
    $strActivo = isset($arrInfo["ACTIVO"]) ? $arrInfo["ACTIVO"] : "";
    $strfk_nivel = isset($arrInfo["FK_NIVEL"]) ? $arrInfo["FK_NIVEL"] : "";
    
    
    $strQuery = "SELECT * FROM cllg_academico_secciones WHERE 1";
    $qTMP = $db->query($strQuery);
    while ( $rTMP = $db->fetch_array($qTMP) ) { 
        $arrSecciones[$rTMP["ID"]] = $rTMP;        
    }
    $db->free_result($qTMP);
    
    ?>
    <br>
    <br>
    <div style="width: 100px; height: 30px; float: left;"><b>Nivel Academico</b></div>
    <div style="width: 400px; height: 30px; float: left;">
        <select name="txtAcademicoGrados_fk-nivel">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_niveles WHERE ACTIVO = 'Y'";
            $qTMP = $db->query($strQuery);
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                $strSelected = ( $strfk_nivel==$rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?>><?php print $rTMP["CODIGO"]; ?> - <?php print $rTMP["NOMBRE"]; ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Codigo</b></div>
    <div style="width: 400px; height: 30px; float: left;"><input type="text" name="txtAcademicoGrados_codigo" value="<?php print $strCodigo; ?>"></div>
    <div style="width: 100px; height: 30px; float: left;"><b>Nombre</b></div>
    <div style="width: 400px; height: 30px; float: left;"><input type="text" name="txtAcademicoGrados_Nombre" value="<?php print $strNombre; ?>"></div>
    <div style="width: 100px; height: 30px; float: left;"><b>Secciones</b></div>
    <div style="width: 400px; height: 30px; float: left;">
        <?php
        reset($arrSecciones);
        while( $arrTMP = each($arrSecciones) ) {
            $strChecked = ( isset($arrInfoSecciones[$arrTMP["key"]]) ) ? "checked" : "";
            ?>
            <input type="checkbox" name="chkAcademicoGradosSecciones_<?php print $arrTMP["key"]; ?>" value="<?php print $arrTMP["key"]; ?>" <?php print $strChecked; ?> ><?php print $arrTMP["value"]["NOMBRE"]; ?> 
            <?php
        }
        ?>
        
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Activo</b></div>
    <div style="width: 400px; height: 30px; float: left;">Si&nbsp;<input type="radio" name="radAcademicoGrados_activo" value="Y" <?php print ($strActivo=="Y")? "checked" : ""; ?>>&nbsp;No<input type="radio" name="radAcademicoGrados_activo" value="N" <?php print ($strActivo=="N")? "checked" : ""; ?>></div>
    <?php
    
}
function fntAcademicoGradosReporte($db){

    //VARIBLES
    $strId = "";
    $strQuery = "";
    //VARIBLES
    
    $strQuery = "SELECT * FROM cllg_academico_grados ORDER BY CODIGO";
    $qTMP = $db->query($strQuery);
    $intNumRows = $db->num_rows;
    
    ?>
    <style type="">
    table {
        border-collapse: collapse;
    }
    table td{
        border: 1px solid #AAAAAA;        
    }
    .oddtr {
        background-color:#FFFFFF;
    }
    .eventr {
        background-color:#FFFFFF;
    }
    .trover {
        background-color: #ECEAE0;
    }
    .trclick {
        background-color: #ECEAE0;
    }
    </style>
    <br>
    <br>
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tbody>
            <td align="center" width="20%" style="background-color: #ECEAE0;"><b>Codigo</b></td>
            <td width="80%" style="background-color: #ECEAE0;"><b>Nombre</b></td>
        </tbody>
    </table>
    <table width="100%" cellpadding="2" cellspacing="0" border="0" id="tblAcademicoGrados">   
        <?php    
        if( $intNumRows ) {
        
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                ?>
                <tr id="<?php print $rTMP["ID"]; ?>">
                    <td align="center" width="20%"><?php print $rTMP["CODIGO"]; ?></td>
                    <td width="80%"><?php print $rTMP["NOMBRE"]; ?></td>
                </tr>        
                <?php
            }
            
        }
        $db->free_result($qTMP);
        ?>
    </table>
    <script type="">
    //these two line adds the color to each different row
    $("#tblAcademicoGrados tr:even").addClass("eventr");;
    $("#tblAcademicoGrados tr:odd").addClass("oddtr");;
    //handle the mouseover , mouseout and click event
    $("#tblAcademicoGrados tr").mouseover(function() {
        $(this).addClass("trover");
    }).mouseout(function() {
        $(this).removeClass("trover");
    }).click(function() {
        //$("tr[id!='"+$(this).attr("id")+"']").removeClass("trover");
        //$(this).addClass("trover");        
        //$(this).toggleClass("trclick");
        $("input[name='hdnAcademicoGrados_Id']").val( $(this).attr("id") );
        fntAcademicoGradosContenido();
    });
    </script>
    <?php
    
}
function fntAcademicoGradosGrabar($db){

    $strId = isset($_POST["hdnAcademicoGrados_Id"]) ? $_POST["hdnAcademicoGrados_Id"] : "";
    $strFields = "";
    $strValues = "";
    
    while( $arrPost = each($_POST) )  {
        $arrExplode = explode( "_", $arrPost["key"]);
        
        if( $arrExplode[0] == "txtAcademicoGrados" ) {
            if( empty($strId) ) {
                $strFields .= (empty($strFields)) ? "" : ","; 
                $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) );
                
                $strValues .= empty( $strValues ) ? "" : ",";
                $strValues .= "'{$arrPost["value"]}'";
            }
            else {
                $strFields .= (empty($strFields)) ? "" : ",";
                $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) ). " = '{$arrPost["value"]}'"; 
                
            }
        }

    }
    if( empty($strId) ) {
        $db->query("INSERT INTO cllg_academico_grados ({$strFields}) VALUES ({$strValues})");
        $intID = mysql_insert_id ( $db->link_id );
        $db->query("DELETE FROM cllg_academico_grados_secciones WHERE FK_GRADO = '{$intID}' ");
        reset($_POST);
        while( $arrPost = each($_POST) )  {
            $arrExplode = explode( "_", $arrPost["key"]);
            if( $arrExplode[0] == "chkAcademicoGradosSecciones" ) {
                $db->query("REPLACE INTO cllg_academico_grados_secciones ( FK_GRADO, FK_SECCION ) VALUES ( '{$intID}', '{$arrPost["value"]}' ) ");
            }
            
        }
    }
    else {
        $db->query("UPDATE cllg_academico_grados SET {$strFields} WHERE ID = '{$strId}'");
        $db->query("DELETE FROM cllg_academico_grados_secciones WHERE FK_GRADO = '{$strId}' ");
        reset($_POST);
        while( $arrPost = each($_POST) )  {
            $arrExplode = explode( "_", $arrPost["key"]);
            if( $arrExplode[0] == "chkAcademicoGradosSecciones" ) {
                $db->query("REPLACE INTO cllg_academico_grados_secciones ( FK_GRADO, FK_SECCION ) VALUES ( '{$strId}', '{$arrPost["value"]}' ) ");
            }
            
        }
    }
    
    
}
?>